﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BTL_SQL
{
    public partial class FormMon : Form
    {
        public FormMon()
        {
            InitializeComponent();
        }

        public SqlConnection getconnect()
        {
            return new SqlConnection("Data Source=trantai-pc;Initial Catalog=QUANLY_TRUONG_THPT;Integrated Security=True");
        }

        private void FormMon_Load(object sender, EventArgs e)
        {
            SqlConnection con = getconnect();
            SqlDataAdapter ad = new SqlDataAdapter("Pro_Hienthi_Mon", con);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            dgMon.DataSource = dt;
        }
        int dong;
        private void dgMon_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                dong = e.RowIndex;
                txtMaMonHoc.Text = dgMon.Rows[dong].Cells[0].Value.ToString();
                txtTenMonHoc.Text = dgMon.Rows[dong].Cells[1].Value.ToString();
                txtHeSo.Text = dgMon.Rows[dong].Cells[2].Value.ToString();
            }
            catch(Exception){}
        }

        private void butThoat_Click(object sender, EventArgs e)
        {
            this.Close();
            FormHeThong ht4 = new FormHeThong();
            ht4.Show();
        }

        private void butHien_Click(object sender, EventArgs e)
        {
            FormMon_Load(sender, e);
        }

        private void butThem_Click(object sender, EventArgs e)
        {
            SqlConnection con = getconnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("Pro_Them_Mon", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@mamh", SqlDbType.NVarChar, 10).Value = txtMaMonHoc.Text;
            cmd.Parameters.Add("@tenmh", SqlDbType.NVarChar, 10).Value = txtTenMonHoc.Text;
            cmd.Parameters.Add("@heso", SqlDbType.Int).Value = int.Parse(txtHeSo.Text);
            cmd.ExecuteNonQuery();
            FormMon_Load(sender, e);
        }

        private void butSua_Click(object sender, EventArgs e)
        {
            SqlConnection con = getconnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("Pro_Sua_Mon", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@mamh", SqlDbType.NVarChar, 10).Value = txtMaMonHoc.Text;
            cmd.Parameters.Add("@tenmh", SqlDbType.NVarChar, 10).Value = txtTenMonHoc.Text;
            cmd.Parameters.Add("@heso", SqlDbType.Int).Value = int.Parse(txtHeSo.Text);
            cmd.ExecuteNonQuery();
            FormMon_Load(sender, e);
        }

        private void butXoa_Click(object sender, EventArgs e)
        {
            SqlConnection con = getconnect();
            con.Open();
            SqlCommand cmd = new SqlCommand("Pro_Xoa_Mon", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@mamh", SqlDbType.NVarChar, 10).Value = txtMaMonHoc.Text;
            cmd.ExecuteNonQuery();
            FormMon_Load(sender, e);
        }
    }
}
